﻿using Coldairarrow.Entity.DTO;
using Coldairarrow.Util;
using Coldairarrow.Util.Helper;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Helpers;
namespace Coldairarrow.Api.Controllers.Base_Manage
{
    [Route("/Base_Manage/[controller]/[action]")]
    [ApiController]
    public class ImageController : BaseController
    {
        public readonly ILogger<ImageController> Logger = null;
        readonly IConfiguration _configuration;
        private IHostingEnvironment hostingEnv;
        public ImageController(ILogger<ImageController> logger, IHostingEnvironment env, IConfiguration configuration)
        {
            Logger = logger;
            this.hostingEnv = env;
            _configuration = configuration;
        }

        /// <summary>
        /// 图像上传加水印 朱庆龙 2022/4/19  18:06  上传到桶
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UploadImageAsync(string text)
        {
            string address = text + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            var file = Request.Form.Files.FirstOrDefault();
            string fileName = file.FileName;
            string path = $"/Upload/{file.FileName}";
            string _path = $"\\{file.FileName}";
            string physicPath = GetAbsolutePath($"~{path}");
            string dir = Path.GetDirectoryName(physicPath);
            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);
            var name = Path.GetFileName(file.FileName);
            using (FileStream fs = new FileStream(physicPath, FileMode.Create))
            {
                file.CopyTo(fs);
            }
            if (name != null)
            {
                using (var stream = new MemoryStream())
                {
                    await file.CopyToAsync(stream);  
                    // Add watermark
                    var watermarkedStream = new MemoryStream();
                    using (var img = Image.FromStream(stream))
                    {
                        using (var graphic = Graphics.FromImage(img))
                        {          
                            //设置比字体大小
                            var font = new Font(FontFamily.GenericSansSerif, 30, FontStyle.Bold, GraphicsUnit.Pixel);
                            //设置字体RGB颜色
                            var color = Color.FromArgb(128, 255, 255, 255);
                            var brush = new SolidBrush(color);
                            var point = new Point(10,10);
                            //水印内容进行描绘
                            graphic.DrawString(address, font, brush, point);
                            img.Save(watermarkedStream, ImageFormat.Jpeg);
                        }
                       
                        img.Save(hostingEnv.WebRootPath + "/" + name);

                    }
                }
            }
            //初始化minio
            MinioHelper minio = new MinioHelper();
            var url = await minio.UploadFile("shengzhuo", hostingEnv.WebRootPath+"/"+name);
            var res = new
            {
                name = name,
                status = "done",
                thumbUrl = url,
                url = url,
                phy = dir + _path
            };
            return JsonContent(res.ToJson());


        }


        [HttpPost]
        public async Task<IActionResult> UploadIpImageAsync(string text)
        {
            string address = text + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            var file = Request.Form.Files.FirstOrDefault();
            string fileName = file.FileName;
            string path = $"/Upload/{file.FileName}";
            string _path = $"\\{file.FileName}";
            string physicPath = GetAbsolutePath($"~{path}");
            string dir = Path.GetDirectoryName(physicPath);
            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);
            var name = Path.GetFileName(file.FileName);
            //using (FileStream fs = new FileStream(physicPath, FileMode.Create))
            //{
            //    file.CopyTo(fs);
            //}
            //if (name != null)
            //{
                using (var stream = new MemoryStream())
                {
                    await file.CopyToAsync(stream);
                    // Add watermark
                    var watermarkedStream = new MemoryStream();
                    using (var img = Image.FromStream(stream))
                    {
                        using (var graphic = Graphics.FromImage(img))
                        {
                            //设置比字体大小
                            var font = new Font(FontFamily.GenericSansSerif, 30, FontStyle.Bold, GraphicsUnit.Pixel);
                            //设置字体RGB颜色
                            var color = Color.FromArgb(128, 255, 255, 255);
                            var brush = new SolidBrush(color);
                            var point = new Point(10, 10);
                            //水印内容进行描绘
                            graphic.DrawString(address, font, brush, point);
                            img.Save(watermarkedStream, ImageFormat.Jpeg);
                        }

                        img.Save(physicPath + "/" + name);

                    }
                }
          //  }
          
            string url = $"{_configuration["WebRootUrl"]}{physicPath}";
            var res = new
            {
                name = name,
                status = "done",
                thumbUrl = url,
                url = url,
                phy = dir + _path
            };
            return JsonContent(res.ToJson());


        }

    }
    }

